package com.meessiom.train.crm.utils;

import com.meessiom.train.crm.fundsrecoverymanagement.model.Fundsrecoverymanagement;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.concurrent.TimeUnit;

public class RDU1 {
    private static final Random random = new Random();
    private static final String[] COUNTRIES = {"中国", "美国", "日本", "德国", "英国", "法国", "加拿大", "澳大利亚", "韩国", "新加坡"};
    private static final String[] CUSTOMERS = {"阿里巴巴", "腾讯", "华为", "苹果", "谷歌", "亚马逊", "微软", "特斯拉", "三星", "索尼"};
    private static final String[] ORDERS = {"ORD2023001", "ORD2023002", "ORD2023003", "ORD2023004", "ORD2023005",
            "ORD2023006", "ORD2023007", "ORD2023008", "ORD2023009", "ORD2023010"};
    private static final String[] FREQUENCIES = {"一次性", "每周", "每月", "每季度", "每年", "不定期"};
    private static final String[] PLANS = {"计划A", "计划B", "计划C", "计划D", "计划E"};
    private static final String[] METHODS = {"银行转账", "支付宝", "微信支付", "现金", "支票", "信用证"};
    private static final String[] TYPES = {"正常回款", "提前回款", "延迟回款", "部分回款", "全额回款"};
    private static final String[] STATUSES = {"未逾期", "逾期1-30天", "逾期31-60天", "逾期61-90天", "逾期90天以上"};
    private static final String[] COLLECTORS = {"张三", "李四", "王五", "赵六", "钱七", "孙八", "周九", "吴十"};
    private static final String[] REMARKS = {"正常回款", "客户要求延期", "财务系统问题", "银行处理延迟", "无"};
    private static final String[] SYSTEM_NUMBERS = {"SYS001", "SYS002", "SYS003", "SYS004", "SYS005", "SYS006", "SYS007"};
    private static final String[] APPROVERS = {"审批员A", "审批员B", "审批员C", "审批员D", "审批员E"};
    private static final String[] SUBMITTERS = {"提交员1", "提交员2", "提交员3", "提交员4", "提交员5"};
    private static final String[] NOTIFICATION_RECIPIENTS = {"财务部", "销售部", "管理层", "客户服务部", "全部部门"};

    public static Fundsrecoverymanagement generateRandomFundsRecovery() {
        Fundsrecoverymanagement record = new Fundsrecoverymanagement();

        // 设置国家日期 (过去365天内随机日期)
        record.setCountryDate(generateRandomDate());

        // 设置回款金额 (1000-100000之间的随机数，保留2位小数)
        record.setPaymentAmount(Math.round(random.nextDouble() * 99000 + 1000) / 100.0);

        // 设置关联客户
        record.setRelatedCustomer(CUSTOMERS[random.nextInt(CUSTOMERS.length)]);

        // 设置关联订单
        record.setRelatedOrder(ORDERS[random.nextInt(ORDERS.length)]);

        // 设置回款频次
        record.setPaymentFrequency(FREQUENCIES[random.nextInt(FREQUENCIES.length)]);

        // 设置回款计划
        record.setPaymentPlan(PLANS[random.nextInt(PLANS.length)]);

        // 设置付款方式
        record.setPaymentMethod(METHODS[random.nextInt(METHODS.length)]);

        // 设置回款类型
        record.setPaymentType(TYPES[random.nextInt(TYPES.length)]);

        // 设置逾期状态
        record.setOverdueStatus(STATUSES[random.nextInt(STATUSES.length)]);

        // 设置逾期天数 (根据逾期状态决定)
        String status = record.getOverdueStatus();
        if (status.equals("未逾期")) {
            record.setOverdueDays(0);
        } else if (status.equals("逾期1-30天")) {
            record.setOverdueDays(random.nextInt(30) + 1);
        } else if (status.equals("逾期31-60天")) {
            record.setOverdueDays(random.nextInt(30) + 31);
        } else if (status.equals("逾期61-90天")) {
            record.setOverdueDays(random.nextInt(30) + 61);
        } else {
            record.setOverdueDays(random.nextInt(365) + 91);
        }

        // 设置收款人员
        record.setCollectionPerson(COLLECTORS[random.nextInt(COLLECTORS.length)]);

        // 设置备注信息
        record.setRemarks(REMARKS[random.nextInt(REMARKS.length)]);

        // 设置系统编号
        record.setSystemNumber(SYSTEM_NUMBERS[random.nextInt(SYSTEM_NUMBERS.length)]);

        // 设置审批人员
        record.setApprover(APPROVERS[random.nextInt(APPROVERS.length)]);

        // 设置提交时间 (比国家日期早1-30天)
        record.setSubmissionTime(generateDateBefore(record.getCountryDate(), random.nextInt(30) + 1));

        // 设置提交人员
        record.setSubmitter(SUBMITTERS[random.nextInt(SUBMITTERS.length)]);

        // 设置通知地点
        record.setNotificationRecipients(NOTIFICATION_RECIPIENTS[random.nextInt(NOTIFICATION_RECIPIENTS.length)]);

        // 设置审批时间 (比提交时间晚1-7天)
        record.setApprovalTime(generateDateAfter(record.getSubmissionTime(), random.nextInt(7) + 1));

        return record;
    }

    private static Date generateRandomDate() {
        long now = System.currentTimeMillis();
        long randomPast = now - TimeUnit.DAYS.toMillis(random.nextInt(365));
        return new Date(randomPast);
    }

    private static Date generateDateBefore(Date date, int daysBefore) {
        return new Date(date.getTime() - TimeUnit.DAYS.toMillis(daysBefore));
    }

    private static Date generateDateAfter(Date date, int daysAfter) {
        return new Date(date.getTime() + TimeUnit.DAYS.toMillis(daysAfter));
    }

//    public static void main(String[] args) {
//        // 示例：生成10条随机记录
//        for (int i = 0; i < 10; i++) {
//            Fundsrecoverymanagement record = generateRandomFundsRecovery();
//            System.out.println("记录 " + (i+1) + ":");
//            System.out.println("国家日期: " + formatDate(record.getCountryDate()));
//            System.out.println("回款金额: " + record.getPaymentAmount());
//            System.out.println("关联客户: " + record.getRelatedCustomer());
//            System.out.println("关联订单: " + record.getRelatedOrder());
//            System.out.println("回款频次: " + record.getPaymentFrequency());
//            System.out.println("付款方式: " + record.getPaymentMethod());
//            System.out.println("逾期状态: " + record.getOverdueStatus());
//            System.out.println("逾期天数: " + record.getOverdueDays());
//            System.out.println("----------------------------------");
//        }
//    }

    private static String formatDate(Date date) {
        return new SimpleDateFormat("yyyy-MM-dd").format(date);
    }
}